﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using ClipperHouse.jQuery.Base;

namespace ClipperHouse.jQuery
{
	/// <summary>
	/// jQuery animate() effect. See http://docs.jquery.com/Effects/animate
	/// </summary>
	public class Animate : JQuery
	{
		private const string animateParamsParamName = "AnimateParams";
		private const string cssOptionsParamName = "CssOptions";
		private const string easingOptionName = "easing";
		private const string durationOptionName = "duration";
		private const string queueOptionName = "queue";

		public Animate()
		{
			this.Params.Clear();
			Params[animateParamsParamName] = String.Empty;
		}

		public override string jQueryMethodName
		{
			get { return "animate"; }
		}

		protected override void OnLoad(EventArgs e)
		{
			if (isQueueSet)
			{
				Options["complete"] = this.CallbackScript.Text.WhitespaceToSpace().Trim().ToFunctionExpression();
				this.CallbackScript.Text = String.Empty;
				Params[cssOptionsParamName] = this.OptionsExpression;
			}
			else
			{
				Params[durationOptionName] = this.Duration;
				Params[easingOptionName] = this.Easing;
			}
			base.OnLoad(e);
		}

		/// <summary>
		/// A string representing one of the three predefined speeds/durations ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
		/// (taken from jQuery docs at: http://docs.jquery.com/Effects)
		/// </summary>
		public string Duration
		{
			get
			{
				return Options[durationOptionName];
			}
			set
			{
				Options[durationOptionName] = (string)value;
			}
		}

		public string Easing
		{
			get
			{
				return Options[easingOptionName];
			}
			set
			{
				Options[easingOptionName] = (string)value;
			}
		}

		public bool Queue
		{
			get
			{
				return Boolean.Parse(Options[queueOptionName]);
			}
			set
			{
				Options[queueOptionName] = value.ToString();
				isQueueSet = !String.IsNullOrEmpty(Options[queueOptionName]);
			}
		}
		private bool isQueueSet = false;

		[
		DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
		PersistenceMode(PersistenceMode.InnerProperty)
		]
		public InnerProperty CssParams
		{
			get
			{
				return _CssParams;
			}
			set
			{
				_CssParams = value;
				Params[animateParamsParamName] = _CssParams.Text.CleanCssParamsList().ToParamsExpression();
			}
		}
		private InnerProperty _CssParams;
	}
}
